/**
 * 
 */
package com.kexinde.wjifen.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.kexinde.wjifen.dao.impl.ExchangeLogDao;
import com.kexinde.wjifen.dao.impl.GiftDao;
import com.kexinde.wjifen.dao.impl.NormalUserDao;
import com.kexinde.wjifen.dao.impl.ShopDao;
import com.kexinde.wjifen.model.ExchangeLog;
import com.kexinde.wjifen.model.Gift;
import com.kexinde.wjifen.model.NormalUser;
import com.kexinde.wjifen.model.Shop;
import com.kexinde.wjifen.model.ShopPoint;
import com.kexinde.wjifen.service.IExchangeLogManager;

/**
 * @author vincent
 *
 */
@Component("exchangeLogManager")
public class ExchangeLogManager implements IExchangeLogManager {

	private ExchangeLogDao exchangeLogDAO;
	private GiftDao giftDao;
	private NormalUserDao normalUserDao;

	private ShopDao shopDao;

	@Override
	public boolean add(Gift gift, Shop shop, NormalUser normalUser) {
		// TODO Auto-generated method stub
		boolean result=false;
		//先扣除相应积分
		boolean minurs=false;
		List<ShopPoint> shopPoints=normalUser.getShopPoints();
		for(int i=0;i<shopPoints.size();i++){
			if(shop.getId()==shopPoints.get(i).getId()){
				if(shopPoints.get(i).getPoint()>=gift.getNeedPoint()){
					shopPoints.get(i).setPoint(shopPoints.get(i).getPoint()-gift.getNeedPoint());
					minurs=true;
					break;
				}
			}
		}
		//扣除积分成功则添加对应记录
		if(true==minurs){
			ExchangeLog log=new ExchangeLog();
			log.setGift(gift);
			log.setNormalUser(normalUser);//加属性
			Date time=new Date();
			log.setTime(time);
			if(this.exchangeLogDAO.doInsert(log))
				result=true;
		}
		//添加记录成功后返回
		return result;
	}

	@Override
	public List<ExchangeLog> showAllByShop(long shopId) {
		// TODO Auto-generated method stub
		Shop shop=this.shopDao.findById(shopId);
		List<Gift> gifts=shop.getGifts();
		List<ExchangeLog> logList=null;
		if(null!=gifts){
			logList=gifts.get(0).getExchangeLogs();
			for(int i=1;i<gifts.size();i++){
				logList.addAll(gifts.get(i).getExchangeLogs());
			}
		}
		return logList;
	}

	@Override
	public List<ExchangeLog> showAllByGift(long shopId, long giftId) {
		// TODO Auto-generated method stub
		Gift gift=this.giftDao.findById(giftId);//giftDao要加findById
		return gift.getExchangeLogs();
	}

	@Override
	public List<ExchangeLog> showAllByUser(String normalUserUserName) {
		// TODO Auto-generated method stub
		NormalUser normalUser=this.normalUserDao.findByName(normalUserUserName);//normalUserDao要加findByName
		return normalUser.getExchangeLogs();
	}

	public ShopDao getShopDao() {
		return shopDao;
	}
	@Resource
	public void setShopDao(ShopDao shopDao) {
		this.shopDao = shopDao;
	}

	public GiftDao getGiftDao() {
		return giftDao;
	}
	@Resource
	public void setGiftDao(GiftDao giftDao) {
		this.giftDao = giftDao;
	}

	public NormalUserDao getNormalUserDao() {
		return normalUserDao;
	}
	@Resource
	public void setNormalUserDao(NormalUserDao normalUserDao) {
		this.normalUserDao = normalUserDao;
	}

	public ExchangeLogDao getExchangeLogDAO() {
		return exchangeLogDAO;
	}
	@Resource
	public void setExchangeLogDAO(ExchangeLogDao exchangeLogDAO) {
		this.exchangeLogDAO = exchangeLogDAO;
	}

}
